package com.retoc.oawx.service.impl;

import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.retoc.oawx.domain.Evection;
import com.retoc.oawx.domain.Ot;
import com.retoc.oawx.exception.OAException;
import com.retoc.oawx.mapper.UserMapper;
import com.retoc.oawx.service.ApproveService;
import com.retoc.oawx.service.OtService;
import com.retoc.oawx.mapper.OtMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;

/**
* @author retoc
* @description 针对表【tb_ot】的数据库操作Service实现
* @createDate 2022-06-27 19:40:02
*/
@Service
public class OtServiceImpl extends ServiceImpl<OtMapper, Ot>
    implements OtService{

    @Autowired
    OtMapper otMapper;

    @Autowired
    UserMapper userMapper;

    @Autowired
    ApproveService approveService;

    @Value("${oawx.receiveNotify}")
    private String receiveNotify;

    @Value("${workflow.urla}")
    private String workflow;

    @Override
    public int deleteByid(Integer id) {
        approveService.deleteProcess(otMapper.selectInstanceIdbyId(id));
        return otMapper.deleteByid(id);
    }

    @Override
    public ArrayList<HashMap> selectOtListByCreator(HashMap map) {
        return otMapper.selectOtListByCreator(map);
    }

    @Override
    public ArrayList<HashMap> selectOldOtListByCreator(HashMap map) {
        return otMapper.selectOldOtListByCreator(map);
    }

    @Override
    public HashMap selectOtById(Integer id) {
        return otMapper.selectOtById(id);
    }

    @Override
    public Ot selectOtByid(int id) {
        return otMapper.selectOtByid(id);
    }

    @Override
    public int createOt(Ot ot) {
        int gmId = userMapper.searchGmId();
        if (gmId==ot.getCreaterId()){
            ot.setStatus(1);
        }
        int row = otMapper.createEvection(ot);
        if (row!=1){
            throw new OAException("创建用车单失败");
        }
        startWorkflow(ot);
        return row;
    }

    @Override
    public void approveOtByInstanceId(String instanceId) {
        otMapper.approveOtByInstanceId(instanceId);
    }

    @Override
    public Ot selectOtByInstanceId(String instanceId) {
        return otMapper.selectOtByInstanceId(instanceId);
    }

    @Override
    public int updateOt(Ot ot) {
        approveService.deleteProcess(otMapper.selectInstanceIdbyId(ot.getId()));
        int i = otMapper.updateOt(ot);
        startWorkflow(ot);
        return i;
    }

    public void startWorkflow(Ot ot){
        JSONObject json = new JSONObject();
        json.set("url", receiveNotify);
        json.set("createrId", ot.getCreaterId());
        int deptManagerId = userMapper.searchDeptManagerId(ot.getCreaterId());
        json.set("managerId", deptManagerId);
        Date startTime = ot.getStartTime();
        Date endTime = ot.getEndTime();
        json.set("dates", startTime);
        long between = DateUtil.between(startTime, endTime, DateUnit.HOUR);
        json.set("uuid", ot.getUuid());
        json.set("hours", (int) between);
        json.set("types", "ot");
        String url = workflow + "/ot/otRequest";
        HttpResponse resp = HttpRequest.post(url).header("Content-Type", "application/json").body(json.toString())
                .execute();
        if (resp.getStatus() == 200) {
            JSONObject json1 = JSONUtil.parseObj(resp.body());
            JSONObject result = JSONUtil.parseObj(json1.getStr("result"));
            String instanceId = result.getStr("instanceId");
            HashMap param = new HashMap();
            param.put("uuid", ot.getUuid());
            param.put("instanceId", instanceId);
            int row1 = otMapper.setInstanceId(param);
            if (row1!=1){
                throw new OAException("保存会议工作流失败");
            }
        }
    }
}




